package com.yami.shop.api.util;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.sun.org.apache.xpath.internal.operations.Bool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class JifenmallAuthUtil {

    // private final static String login_url = "http://core.qasa.chtwm.com/api/user/pc/public/login";
    @Value("${jifen.server}")
    private String server ;
    @Value("${jifen.loginUrl}")
    private String login_url;


    /**
     * 鉴权
     *
     * @param principal   用户名
     * @param credentials 密码
     * @return
     */
    public  void auth(String principal, String credentials) {
       log.info("server+login_url:"+server+login_url);

        String encryptUserName = Encryptor.encrypt(principal);
        String encryptPassword = Encryptor.encrypt(credentials);
        String encryptData = Encryptor.encrypt(principal+credentials);
        String param = String.format("encryptUserName=%s&encryptPassword=%s&encryptData=%s", encryptUserName, encryptPassword, encryptData);
        String responseStr = HttpUtil.post(server + login_url, param);

        JifenmallResponse response = JSONUtil.toBean(responseStr, JifenmallResponse.class);
        // System.out.println("登录结果："+response.toString());
        if (response.getStatus().equals("0000")) {
            return;
        } else {
            throw new RuntimeException("登陆失败：用户名或密码错误。" + response.toString());
        }
    }
}
